home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 235_01 / ovmenu.c < prev    next >
Text File  |  1987-06-16  |  8KB  |  223 lines

  1. /*  007  28-Jan-87  ovmenu.c
  2.  
  3.         Copyright (c) 1987 by Blue Sky Software.  All rights reserved.
  4. */
  5.  
  6. #include <stdio.h>
  7. #include "ov.h"
  8.  
  9. MENU *save_top;
  10. MENU_STATE curmenu, savmenu;
  11. static int force_selection = FALSE;
  12.  
  13. extern MENU *top_menu;
  14.  
  15.  
  16. /******************************************************************************
  17.                       M E N U _ S A V E / R E S T O R E
  18.  ******************************************************************************/
  19.  
  20. menu_save() {          /* save the current menu state */
  21.  
  22.    savmenu = curmenu;
  23.    save_top = top_menu;
  24. }
  25.  
  26. menu_restore() {       /* restore saved menu state */
  27.  
  28.    curmenu = savmenu;
  29.    top_menu = save_top;
  30.    force_selection = TRUE;
  31. }
  32.  
  33.  
  34. /******************************************************************************
  35.  **                       M E N U _ I N I T                                  **
  36.  *****************************************************************************/
  37.  
  38. menu_init() {          /* initialize the menu */
  39.  
  40.    menu_display(curmenu.current_menu = top_menu); /* display the initial menu */
  41.    menu_select(curmenu.current_selection = 0);    /* select the first option */
  42.    menu_prompt(curmenu.current_selection);        /* display selections prompt */
  43. }
  44.  
  45.  
  46. /******************************************************************************
  47.  **                      M E N U _ D I S P L A Y                             **
  48.  *****************************************************************************/
  49.  
  50. menu_display(menu)     /* display a menu */
  51. register MENU *menu;
  52. {
  53.    int col = 0;
  54.    register int i;
  55.  
  56.    gotorc(MENU_ROW,0);
  57.  
  58.    /* display each of the menu selections, for each one, remember where it
  59.       is on the menu row and how long it is.  This info is used by the
  60.       menu_select/deselect functions.  The end of the menu is denoted by
  61.       a set of NULL entries,  finished when this entry is found.  Put the
  62.       number of selections for this menu into the global variable
  63.       number_selections for other routines to use */
  64.  
  65.    for (i = 0; menu->choice != NULL; i++, menu++) {
  66.       curmenu.selection[i].position = col;
  67.       curmenu.selection[i].length = strlen(menu->choice);
  68.       disp_str(menu->choice);
  69.       disp_str("  ");
  70.       col += curmenu.selection[i].length + 2;
  71.    }
  72.  
  73.    clr_eol();
  74.  
  75.    curmenu.number_selections = i;      /* remember how many selections in menu */
  76.  
  77. }
  78.  
  79.  
  80. /******************************************************************************
  81.  **                      M E N U _ S E L E C T                               **
  82.  *****************************************************************************/
  83.  
  84. menu_select(sel)       /* select (highlight) a menu selection */
  85. register int sel;
  86. {
  87.    setvattrib(DIS_HIGH);               /* set selection attribute */
  88.  
  89.    /* display the selection with attribute set */
  90.  
  91.    disp_str_at(curmenu.current_menu[sel].choice,MENU_ROW,
  92.                curmenu.selection[sel].position);
  93.  
  94.    setvattrib(DIS_NORM);               /* return to normal attribute */
  95. }
  96.  
  97.  
  98. /******************************************************************************
  99.  **                      M E N U _ P R O M P T                               **
  100.  *****************************************************************************/
  101.  
  102. menu_prompt(sel)       /* display a selection's prompt string */
  103. int sel;
  104. {
  105.    disp_str_at(curmenu.current_menu[sel].prompt,MENU_ROW+1,0);
  106.    clr_eol();
  107. }
  108.  
  109.  
  110. /******************************************************************************
  111.  **                      M E N U _ D E S E L E C T                           **
  112.  *****************************************************************************/
  113.  
  114. menu_deselect(sel)     /* deselect (unhighlight) a menu selection */
  115. register int sel;
  116. {
  117.    /* display the selection with normal attribute */
  118.  
  119.    disp_str_at(curmenu.current_menu[sel].choice,MENU_ROW,
  120.                curmenu.selection[sel].position);
  121. }
  122.  
  123.  
  124. /******************************************************************************
  125.  **                   M E N U _ A D V A N C E                                **
  126.  *****************************************************************************/
  127.  
  128. menu_advance() {       /* advance the menu selection pointer */
  129.  
  130.    menu_deselect(curmenu.current_selection);    /* deselect previous entry */
  131.  
  132.    /* change the current selection to the next one or wrap around to 0 */
  133.  
  134.    curmenu.current_selection = (curmenu.current_selection <
  135.                                curmenu.number_selections - 1) ?
  136.                                curmenu.current_selection + 1 : 0;
  137.  
  138.    menu_select(curmenu.current_selection);      /* highlight it */
  139.    menu_prompt(curmenu.current_selection);      /* display its prompt */
  140. }
  141.  
  142.  
  143. /******************************************************************************
  144.  **                   M E N U _ B A C K U P                                  **
  145.  *****************************************************************************/
  146.  
  147. menu_backup() {        /* backup the menu selection pointer */
  148.  
  149.    menu_deselect(curmenu.current_selection);    /* deselect previous entry */
  150.  
  151.    /* change the current selection to the previous or warp around to last */
  152.  
  153.    curmenu.current_selection = (curmenu.current_selection > 0) ?
  154.                                curmenu.current_selection - 1 :
  155.                                curmenu.number_selections - 1;
  156.  
  157.    menu_select(curmenu.current_selection);      /* highlight it */
  158.    menu_prompt(curmenu.current_selection);      /* display its prompt */
  159. }
  160.  
  161.  
  162. /******************************************************************************
  163.  **                    M E N U _ D O _ C H A R                               **
  164.  *****************************************************************************/
  165.  
  166. menu_do_char(ch)       /* do the menu selection that starts with ch */
  167. int ch;
  168. {
  169.    register int i;
  170.  
  171.    for (i = 0; i < curmenu.number_selections; i++)
  172.       if (toupper(*curmenu.current_menu[i].choice) == toupper(ch)) {
  173.          if (i != curmenu.current_selection &&
  174.              curmenu.current_menu[i].func != NULL) {
  175.             menu_deselect(curmenu.current_selection);
  176.             menu_select(curmenu.current_selection = i);
  177.             menu_prompt(curmenu.current_selection);
  178.          }
  179.          do_selection(i);
  180.          return(1);
  181.       }
  182.    return(0);
  183. }
  184.  
  185. /******************************************************************************
  186.  **                    M E N U _ D O _ C U R R E N T                         **
  187.  *****************************************************************************/
  188.  
  189. menu_do_current() {    /* do the current menu selection */
  190.  
  191.    do_selection(curmenu.current_selection);
  192. }
  193.  
  194.  
  195. /******************************************************************************
  196.  **                      D O _ S E L E C T I O N                             **
  197.  *****************************************************************************/
  198.  
  199. static int
  200. do_selection(sel)      /* execute a menu selection */
  201. register int sel;
  202. {
  203.    if (curmenu.current_menu[sel].func != NULL)  /* run function if there is 1 */
  204.       (*curmenu.current_menu[sel].func)();
  205.  
  206.    if (!force_selection) {     /* function can force menu not to update */
  207.  
  208.       /* switch to the specified sub menu or top menu if no sub */
  209.  
  210.       if (curmenu.current_menu[sel].sub_menu != NULL)
  211.          curmenu.current_menu = curmenu.current_menu[sel].sub_menu;
  212.       else
  213.          curmenu.current_menu = top_menu;
  214.  
  215.       menu_display(curmenu.current_menu);          /* display menu */
  216.       menu_select(curmenu.current_selection = 0);  /* select item */
  217.       menu_prompt(curmenu.current_selection);      /* display prompt */
  218.  
  219.    } else                              /* menu was forced */
  220.  
  221.       force_selection = FALSE;         /* only good for one shot */
  222. }
  223.